/*--------------------------------------------------------------------------------------------------
Yatak Operation System Project Copyright 2012
Author : Ilya Churaev
Started : 4 May 2012		Updated : 4 May 2012
File Name : queue.c		Purpose : queue
--------------------------------------------------------------------------------------------------*/

#include <libc/queue.h>

void initQueue(queue_t *q)
{
	if(q)
	{
		q->count = 0;
		q->head = q->tail = 0;
	}
}

void enqueue(queue_t *q, char c)
{
	if(q)
	{
		if(q->count<MAX_QUEUE)
		{
			if(q->count==0)
				q->head = q->tail = 0;
			else
				q->tail = (q->tail + 1) % MAX_QUEUE;
			q->mem[q->tail] = c;
			q->count++;
		}
	}
}

char dequeue(queue_t *q)
{
	if(q)
	{
		if(q->count>0)
		{
			char tmp;
			tmp = q->mem[q->head];
			q->head = (q->head + 1) % MAX_QUEUE;
			q->count--;
			return tmp;
		}
	}
	return -1;
}

char enqueueUndo(queue_t *q)
{
	if(q)
	{
		if(q->count>0)
		{
			char tmp;
			tmp = q->mem[q->tail];
			q->count--;
			if(q->tail==0)
				q->tail = MAX_QUEUE-1;
			else
				q->tail--;
			return tmp;
		}
	}
	return -1;
}
